package com.audaque.springboot.foshanupload.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class CronUtils {

    private boolean compensateYn(String cronItem, Long taskId, String cronPrefix) {


        //如果执行时间是今日，则判断是否过时，过时则补偿
        boolean flag = false;
        //秒 分 时 天(月) 月 天(周) 年份
        String[] split1 = cronItem.split(" ");

        //可能是*
        String year1 = split1[6];
        //可能是*
        String month1 = split1[4];
        //不可能是*
        String monthOfDay1 = split1[3];


        Date now = new Date();
        String nowStr = DateUtilsExtLang3.formatDate(now, DateFormatUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        int year = now.getYear();
        year += 1900;
        int month = now.getMonth();
        month++;
        int monthOfDay = now.getDate();

        int hour = now.getHours();
        int minute = now.getMinutes();
        int second = now.getSeconds();
        long time = now.getTime();
        if (Objects.equals(year1, "*") || Integer.parseInt(year1) == year) {
            //只考虑今日
            if (Objects.equals(month1, "*") || Integer.parseInt(month1) == month) {
                if (Objects.equals(Integer.parseInt(monthOfDay1), monthOfDay)) {
                    //今日触发
                    //判断是否过时，过时则补偿
                    //秒 分 时
                    String[] split2 = cronPrefix.split(" ");
                    String second2 = split2[0];
                    String minute2 = split2[1];
                    String hour2 = split2[2];
                    Date now2 = new Date();
                    now2.setHours(Integer.parseInt(hour2));
                    now2.setMinutes(Integer.parseInt(minute2));
                    now2.setSeconds(Integer.parseInt(second2));
                    long time2 = now2.getTime();

                    if (time2 <= time) {

                        Date s = new Date(now.getTime());
                        s.setHours(0);
                        s.setMinutes(0);
                        s.setSeconds(0);
                        LocalDateTime localDateTimes = s.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                        Date e = new Date(now.getTime());
                        e.setDate(e.getDate() + 1);
                        e.setHours(0);
                        e.setMinutes(0);
                        e.setSeconds(0);
                        LocalDateTime localDateTimee = e.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                        //前闭后开
                        List<Object> evaluationBatchList = new ArrayList<>();
                        //evaluationBatchRepository.findByEvaluationTaskIdAndCreateTimeGreaterThanEqualAndCreateTimeLessThan(taskId,localDateTimes, localDateTimee);

                        if (evaluationBatchList.isEmpty()) {
                            flag = true;
                        }

                    }

                }
            }

        }
        return flag;
    }

}
